package com.first.community.controller;

import com.first.community.dto.PaginationDTO;
import com.first.community.model.User;
import com.first.community.service.NotificationService;
import com.first.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class profileController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private NotificationService notificationService;


    @GetMapping("/profile/{action}")
    public String profile(@PathVariable(value = "action",required = false) String action,
                          Model model,
                          HttpServletRequest request,
                          @RequestParam(name = "page",defaultValue = "1") Integer page,
                          @RequestParam(name = "size",defaultValue = "5") Integer size
                          ){
        User user = (User) request.getSession().getAttribute("user");

        if(user==null){
            return  "redirect:/";
        }

        if("questions".equals(action)){
          //我的问题
            model.addAttribute("section","questions");
            model.addAttribute("tag","我的问题");
            PaginationDTO list = questionService.list(user.getId(), page, size);
            model.addAttribute("pagination",list);
        }else if("replies".equals(action)){
            //最新回复
            model.addAttribute("section" ,"replies");
            model.addAttribute("tag","最新回复");
            PaginationDTO paginationDTO = notificationService.list(user.getId(),page,size);
            Long unReadCount = notificationService.getUnreadCount(user.getId());
            model.addAttribute("unReadCount",unReadCount);
            model.addAttribute("pagination",paginationDTO);
        }

        return  "profile";
    }
}
